package io.renren.wechat.service.office;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import io.renren.wechat.param.SyncCorpParam;
import io.renren.wechat.service.suite.dto.Agent;
import io.renren.wechat.support.WechatConstants;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class OfficeService {
    private static final Logger LOGGER = LoggerFactory.getLogger(OfficeService.class);


    public void syncCorp(String corpId, String corpName, List<Agent> agentList) {
        String agentId = "";
        for (Agent agent : agentList) {
            if (WechatConstants.APP_HANQI.equals(agent.getName())) {
                agentId = agent.getAgentId();
                break;
            }
        }

        String url = WechatConstants.OFFICE_HOST + "/api/corp/syncCorp";
        String body = JSONUtil.toJsonStr(SyncCorpParam.convertFor(corpId, corpName, agentId));

        LOGGER.debug("post url = {}, body = {}", url, body);

        String response = HttpUtil.post(url, body);

        LOGGER.debug("response = {}", response);

    }

}
